<script type="text/javascript">
$(function(){
	var pg = App.office.groupIndex.ui, pp = pg.params || {}
		, groupPl = pg.children('.groupPl')
		, groupGd = groupPl.find('.grid')
		, groupAdd = groupPl.seek('add')
		, groupFind = groupPl.seek('find')
		, groupKey = groupPl.seek('key')
		, userPl = pg.children('.userPl')
		, userGd = userPl.find('.grid')
		, ownsCbx = userPl.seek('ownsOnly')
		, userAdd = userPl.seek('add')
		, userFind = userPl.seek('find')
		, userKey = userPl.seek('key')
		, uca = []; //user code arrary
		
	function checkUser(){
		userGd.seek('users').each(function(i, a){
			var me = $(a);
			me.prop('checked', $.inArray(me.val(), uca) >= 0);
		});
	}
		
	function groupUsers(rid){
		$.post(App.office.ctx + '/group/user/list.do', {
			groupId: rid, pageNo: 1, pageSize: 9999
		}, function(rs){
			var ro = mac.eval(rs);
			if (ro.success) {
				uca = $.map(ro.data.list, function(o){
					return o.code;
				});
				checkUser();
			} else {
				App.error(el.html(), ro.data);
			}
		});
	}

	groupGd.mac('grid', {
		cols : [{
			field: 'name', title : '名称', width: 240, sort: true,
			render: function(r, tr, gd) {
				return $('<span class="a"></span>').click(function(){
					if (App.logon.power('office.group.save')){
						App.open({
							ctx: 'office',
							action: 'groupEdit',
							model: true,
							params: r
						}, function(){
							groupGd.load();
						});
					}
				}).append(r.name);
			}
		},{
			field: 'id', title : '操作', width: 90,
			render: function(r, tr, gd){
				var a = $('<div></div>');
				if (App.logon.power('office.group.delete')) {
					$('<span class="a"></span>').click(function(){
						if (confirm('确定要删除这个工作组吗？')) {
							$.post(App.office.ctx + '/group/delete.do', {
								id: r.id
							}, function(rs){
								groupGd.load({ pageNo: 1 });
							});
						}
					}).append('删除').appendTo(a);
					a.append('&nbsp;')
				}
				return a;
			}
		}],
		loader: {
			url: App.office.ctx + '/group/list.do',
			params: {
				pageNo: 1, pageSize: 50,
				corpId: pp.corpId
			},
			autoLoad: true
		},
		afterLoad: function(dd){
			userGd.find(':checkbox').removeProp('checked');
		},
		onRowClick: function(){
			var tr = $(this), sr = groupGd.selected;
			if (tr.hasClass('selected'))
				return true;
			if (sr)
				sr.removeClass('selected');
			sr = groupGd.selected = tr.addClass('selected');
			var rid = groupGd.data[sr.attr('name')].id;
			if (ownsCbx.is(':checked')) {
				userGd.load({
					groupId: rid
				}, App.auth.ctx + '/sys/user/list.do');
			} else {
				groupUsers(rid);
			}
			return true;
		},
		pagerLength: 10
	});

	if (App.logon.power('office.group.save')) {
		groupAdd.click(function(){
			App.open({
				ctx: 'office',
				action: 'groupEdit',
				title: '创建工作组',
				model: true,
				params: {
					corpId: pp.corpId,
					status: 1
				}
			}, function(){
				groupGd.load();
			});
		}).show();
	}
	
	groupFind.click(function() {
		groupGd.load({ key : groupKey.val() });
	});
	groupKey.keydown(function(e){
		if (e.keyCode == 13) {
			groupFind.click();
		}
	});	
	userGd.mac('grid', {
		cols : [{
			field : 'id', title : '<input type="checkbox" name="all" />', width : 24,
			render : function(r, tr, gd) {
				if (App.logon.power('office.group.user.toggle')) {
					var a = $('<input type="checkbox" />').change(function() {
						var b = $(this).is(':checked'), t = b? '添加模块权限':'移除模块权限';
						if (!groupGd.selected) {
							mac.alert('请先选择一个工作组');
							return; //如果左侧没有用户，则点击无效
						}
						var group = groupGd.data[groupGd.selected.attr('name')],
							w = mac.wait(Msg.info.waiting, [ t ]);
						$.post(App.office.ctx + '/group/user/toggle.do', {
							groupId: group.id, //group code
							userIds: r.id, //user code
							status: b? 1:0
						}, function(rs){
							var ro = mac.eval(rs);
							if(ro.success){
								if (ro.data) {
									uca.push(r.code);
								} else {
									var i = $.inArray(r.code, uca);
									uca.splice(i, 1);
								}
							} else {
								App.error(t, ro.data);
							}
							w.close();
						});
					});
					a.attr({
						name : 'users',
						value : r.code
					});
				}
				return a;
			}
		}, {
			field: 'code', title : '编码', width: 135, sort: true,
			render: function(r) {
				return $('<span class="a"></span>').click(function(){
					if (App.logon.power('sys.user.save')) {
						App.open({
							ctx: 'auth',
							action: 'sysUserEdit',
							model: true,
							params: r
						}, function(){
							userGd.load();
						});
					}
				}).append(r.code);
			}
		}, {
			field: 'name', title : '名称', width: 135, sort: true
		}],
		loader: {
			url: App.auth.ctx + '/sys/user/list.do',
			params: { pageNo: 1, pageSize: 50, groupId: '' },
			autoLoad: true
		},
		pagerLength: 10,
		afterLoad: function(dd, po, gd){
			var ac = gd.find(':checkbox');
			ac.prop('checked', false);
			if (ownsCbx.is(':checked')) {
				uca = $.map(dd, function(o){
					return o.code;
				});
				ac.prop('checked', 'checked');
			} else if (uca.length) {
				checkUser();
			}
		}
	});
	if (!App.logon.power('office.group.user.list')){
		userGd.hide();
	}
	userGd.seek('all').click(function(){
		if (!groupGd.selected) {
			/* mac.alert('请先选择一个工作组'); */
			return; //如果左侧没有用户，则点击无效
		}
		var group = groupGd.data[groupGd.selected.attr('name')],
			uids, b = $(this).prop('checked'), fa = userGd.seek('users');
		fa.prop('checked', b);
		uids = fa.map(function(){
			return $(this).val();
		}).get().join(',');
		$.post(App.office.ctx + '/group/user/toggle.do', {
			groupId: group.id,
			userIds: uids,
			status: b? 1:0
		}, function(rs){
			var ro = mac.eval(rs);
			if (ro.success) {
				groupUsers(group.id);
			} else {
				App.error(el.html(), ro.data);
			}
		});
	});
	
	userFind.click(function() {
		userGd.load({ key: userKey.val(), pageNo: 1 });
	});
	userKey.keydown(function(e){
		if (e.keyCode == 13)
			userFind.click();
	});
	ownsCbx.click(function() {
		var el = $(this);
		if (el.is(':checked')) {
			if (groupGd.selected) {
				var k = groupGd.selected.attr('name');
				userGd.load({
					groupId: groupGd.data[k].id,
					pageNo: 1
				}, App.office.ctx + '/group/user/list.do');
			}
		} else {
			userGd.load({ pageNo: 1 }, App.auth.ctx + '/sys/user/list.do');
		}
	});
	
	pg.find('[placeholder]').mac('placeholder');
	
	function adjust(){
		var p = pg.parent(), h = p.height(), w = p.width();
		pg.height(h).width(w);
		w = (w - 8) / 2;
		groupPl.width(w).height(h);
		userPl.width(w).height(h);
		h -= 40;
		groupGd.parent().height(h);
		userGd.parent().height(h);
		groupGd.adjust();
		userGd.adjust();
	}
	$(window).resize(adjust);
	adjust();
});
</script>
<div class="groupPl">
	<div class="head">
		<button name="add" class="hidden">添加新的工作组</button>
		<input name="key" type="text" placeholder="请输入账户名、真实姓名、手机号或邮箱" />
		<button name="find">查找</button>
	</div>
	<div class="body">
		<div class="grid"></div>
	</div>
</div>
<div class="userPl">
	<div class="head">
		<input name="key" type="text" placeholder="请输入编码、名称或备注" />
		<button name="find">查找</button>
	</div>
	<div class="body">
		<div class="grid"></div>
	</div>
</div>
